﻿<div class="layui-card">
	<div class="layui-form layui-card-header layuiadmin-card-header-auto" style="padding-top:0px;padding-bottom:0px;" lay-filter="SearchForm">
		
	</div>

	<div class="layui-card-body">
		<div class="layui-tab layui-tab-brief" lay-filter="tabList">
			<ul class="layui-tab-title">
				<li lay-id="0" class="layui-this">全部</li>
				<li lay-id="1">启用</li>
				<li lay-id="2">禁用</li>
			</ul>
			<div class="layui-tab-content">
				<table id="tblList" lay-filter="tblList"></table>
			</div>
		</div>
		<input type="hidden" id="hidTab" value="0" />
		<script type="text/html" id="toolbarHead">
			<div class="layui-form-item" style="margin: auto;">
				<div class="layui-inline">
					<button class="layui-btn layuiadmin-btn-list" onclick="layer_show('添加内容模型','/Admin/CommonModel/ModelEdit')">添加内容模型</button>
					<a class="layui-btn layuiadmin-btn-list" lay-href="/Admin/CommonModel/ModelTemplateManage#tabList=1">内容模型的模板管理</a>
				</div>
			</div>
		</script>
		<script type="text/html" id="tblStatusCol">
			<input type="checkbox" name="checkOut" value="{{d.ModelID}}" lay-skin="switch" lay-text="已禁用|已启用" lay-filter="CheckOutStatus" {{ (d.IsDisabled) ? "checked" : "" }}>
		</script>
		<script type="text/html" id="tblBarCol">
			<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
			<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
			<a class="layui-btn layui-btn-xs" lay-text="{{d.ModelName }}的字段列表" lay-href="/Admin/CommonModel/FieldManage?ModelType=1&ModelID={{d.ModelID }}">字段列表</a>
			<a class="layui-btn layui-btn-xs" lay-event="AddModelToFieldTemplate">存为模板</a>
		</script>
	</div>
</div>
@Html.AntiForgeryToken()
@section scripts {
	<script type="text/javascript">
		layui.use(['form', 'table', 'element', 'laydate'], function () {
			var table = layui.table;
			var form = layui.form;
			var element = layui.element;
			var laydate = layui.laydate;

			lay('.date-item').each(function () {
				laydate.render({
					elem: this
				});
			});

			//获取hash来切换选项卡，假设当前地址的hash为lay-id对应的值
			var layid = location.hash.replace(/^#tabList=/, '');
			if (layid != null && layid != "") {
				element.tabChange('tabList', layid);
				$("#hidTab").val(layid);
			}
			//监听Tab切换，以改变地址hash值
			element.on('tab(tabList)', function () {
				location.href = location.pathname + "#tabList=" + this.getAttribute('lay-id');
				window.location.reload();
			});

			var myCols = [
				{ field: 'ModelID', width: 100, title: 'ID', align: 'center' },
				{ field: 'ModelName', width: 200, title: '模型名称', align: 'center'},
				{ field: 'Description', minWidth: 150, title: '模型描述', align: 'left' },
				{ field: 'ItemName', width: 200 , title: '项目名称', align: 'center' },
				{ field: 'TableName', width: 200, title: '表名', align: 'center' },
				{ field: 'IsDisabled', width: 100, title: '状态', align: 'center', templet: '#tblStatusCol'},
				{ field: 'ID', width: 300, title: '操作', align: "center", templet: '#tblBarCol' }
			];
			
			//加载表格
			table.render({
				elem: '#tblList'
				, url: '/Admin/CommonModel/GetContentModelList'
				, toolbar: '#toolbarHead'
				, where: GetSearchParam()
				, request: {
					pageName: 'PageNum' //页码的参数名称，默认：page
					, limitName: 'PageSize' //每页数据量的参数名，默认：limit
				}
				, cols: [myCols]
				, parseData: function (res) { //res 即为原始返回的数据
					return {
						"code": 0, //解析接口状态
						"msg": "", //解析提示文本
						"count": res.RecordTotal, //解析数据长度
						"data": res.RowList //解析数据列表
					};
				}
				, limits: [10, 15, 20, 25, 50, 100]
				, limit: 150
				, page: true
			});
			//监听表格操作列
			table.on('tool(tblList)', function (obj) {
				if (obj.event === 'edit') {
					layer_show('编辑模型', '/Admin/CommonModel/ModelEdit?id=' + obj.data.ModelID);
					return false;
				} else if (obj.event === 'del') {
					DeleteSingle(obj.data.ModelID, "/Admin/CommonModel/DelModel");
					return false;
				} else if (obj.event === 'AddModelToFieldTemplate') {
					layer_show('存为模板', '/Admin/CommonModel/ModelTemplateEdit?ModelID=' + obj.data.ModelID);
					return false;
				}
			});
			//监听禁用开关
			form.on('switch(CheckOutStatus)', function (data) {
				var url = '/Admin/CommonModel/UpdateModelStatus';
				if (this.checked) { //设为禁用状态
					var param = { "id": data.value, "Status": true };
					UpdateSwitch(data,url, param);
				} else { //设为启用状态
					var param = { "id": data.value, "Status": false };
					UpdateSwitch(data,url, param);
				}
			});
			//表格重载
			function reloadTable(tableID = 'tblList') {
				table.reload(tableID, {
					page: {
						curr: 1
					}
					, where: GetSearchParam()
				}, 'data');
			};
			function GetSearchParam() {
				var TabStatus = $("#hidTab").val();
				var objParam = { "TabStatus": TabStatus};
				return objParam;
			}
		});
	</script>
}